export function getFileUrl(url: string) {
  const config = useRuntimeConfig()
  const baseApp = 'file'
  const baseApi = config.public.baseApi
  return `${baseApi}/${baseApp}/forward?targetUrl=${encodeURIComponent(url)}`
}

export async function getFileInfo(fileNo: string) {
  const config = useRuntimeConfig()
  const baseApi = config.public.baseApi
  const url = `${baseApi}/file/select/srvfile_attachment_select?fileNo=${fileNo}`
  const req = {
    serviceName: 'srvfile_attachment_select',
    colNames: ['*'],
    condition: [
      { colName: 'file_no', value: fileNo, ruleType: 'eq' },
      { colName: 'is_delete', value: '1', ruleType: 'eq' },
    ],
    relation_condition: {},
  }
  const res = await useHttp.post(url, req)
  return res
}

export function getUrlByFileNo(no: string) {
  const config = useRuntimeConfig()
  const baseApi = config.public.baseApi
  const { bxAuthTicket, isLogin } = useUserStore()
  let url = `${baseApi}/file/download?fileNo=${no}`
  if (isLogin && bxAuthTicket) {
    url = `${url}&bx_auth_ticket=${bxAuthTicket}`
  }
  return url
}

export function getUrlByFilePath(filePath: string) {
  const config = useRuntimeConfig()
  const baseApi = config.public.baseApi
  const { bxAuthTicket, isLogin } = useUserStore()
  let url = `${baseApi}/file/download?filePath=${filePath}`
  if (isLogin && bxAuthTicket) {
    url = `${url}&bx_auth_ticket=${bxAuthTicket}`
  }
  return url
}
